package com.xonro.common.histrans.sfeg.biz;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author:zhuw
 * @Description:
 * @Date 2022/5/5
 */
public class CommTransBiz {
    private static CommTransBiz commTransBiz = new CommTransBiz();

    public static CommTransBiz getInstance() {
        return commTransBiz;
    }

    private final static String CCID = DrpConst.ZZ_CCID;
    private static Map<String, RowMap> userMap;

    private static Map<String, RowMap> userNameMap;

    //    private static
    static {
        List<RowMap> maps = SDK.getCCAPI().getRDSAPI(CCID).getMaps("SELECT * FROM Tb_UserInfo");
        userMap = maps.stream().collect(Collectors.toMap(map -> map.getString("usCode"), Function.identity(), (v1, v2) -> v1));

        userNameMap = maps.stream().collect(Collectors.toMap(map -> map.getString("usName"), Function.identity(), (v1, v2) -> v1));
    }

    public String getUserId(String userCode) {
        RowMap map = userMap.get(userCode);
        if (map == null) {
            return "";
        }
        return map.getString("loginName");
    }

    public  String getUserExchange(String oaUserId){
        if("jiwenhan2020".equals(oaUserId)){
            return "jiwenhao_ky";
        }
        String sql="SELECT * FROM ORGUSER WHERE USERID='"+oaUserId+"_ky'";
        RowMap map = DBSql.getMap(sql);
        if(Objects.isNull(map)){
            return oaUserId;
        }
        return oaUserId+"_ky";
    }

    public  String getUserKY(String id){
        String userId = getUserId(id);
        if(StringUtils.isBlank(userId)){
            return "";
        }
        String kyUserId = getUserExchange(userId);
        return kyUserId;
    }


    public String getUserIdByName(String userName) {
        RowMap map = userNameMap.get(userName);
        if (map == null) {
            return "";
        }
        return map.getString("loginName");
    }

    public RowMap getUser(String userCode) {
        RowMap map = userMap.get(userCode);
        return map;
    }
}
